package com.gitee.sparta.common.helpers;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.gitee.sparta.common.SpartaConstant;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 集合工具类
 *
 * @author zyred
 * @since v 0.0.1
 */
public class CollHelper {

    /**
     * 将字符串转换成 list 集合，根据符号进行切分
     *
     * @param content       字符串
     * @param separator     特殊符号
     * @return              []
     */
    public static List<String> convertList (String content, String separator) {
        String[] split = content.split(separator);
        return Arrays.stream(split).filter(StringUtils::hasText).collect(Collectors.toList());
    }

    public static Set<String> convertSet (String content, String separator) {
        String[] split = content.split(separator);
        return Arrays.stream(split).filter(StringUtils::hasText).collect(Collectors.toSet());
    }


    /**
     * 将完整的 topic 切分
     *
     * @param full      TOPIC_0:12345,TOPIC_1:3242342,TOPIC_2:123123
     * @return          [TOPIC_0,TOPIC_1,TOPIC_2]
     */
    public static Set<String> convertSet (String full) {
        if (StrUtil.isBlank(full)) {
            return Collections.emptySet();
        }

        List<String> fullChannel = CollHelper.convertList(full, SpartaConstant.COMMA);
        Set<String> channels = new HashSet<>();
        for (String task : fullChannel) {
            String channel = task.split(SpartaConstant.COLON)[0];
            channels.add(channel);
        }
        return channels;
    }

    /**
     * 将集合转换成字符串，
     * @param coll          集合
     * @param separator     分割字符串
     * @return              字符串
     */
    public static String convertString (Set<String> coll, String separator) {
        if (CollUtil.isEmpty(coll)) {
            return "";
        }
        List<String> arr = new ArrayList<>(coll.size());
        for (String str : coll) {
            if (str.contains(SpartaConstant.LINE)) {
                String[] split = str.split(SpartaConstant.LINE);
                arr.add(split[0]);
                continue;
            }
            arr.add(str);
        }
        return String.join(separator, arr);
    }


    /**
     * 计算一个数组中，缺少那个数字
     *
     * @param nums  []  |   1   |   1,2 |   1,2,4   |   4   |
     * @return      1   |   2   |   3   |     3     |   1   |
     */
    public static int lackNum (List<Integer> nums) {
        nums.sort(Integer::compareTo);

        if (CollUtil.isEmpty(nums)) {
            return 1;
        }
        if (nums.size() == 1) {
            return nums.get(0) == 1 ? 2 : 1;
        }
        for (int i = 0, len = nums.size(); i < len; i++) {
            if (nums.get(i) != i + 1) {
                return i + 1;
            }
        }
        return nums.get(nums.size() - 1) + 1;
    }


    /**
     * 生成不重复的 5 位随机数
     *
     * @param all   已经生成完毕的数
     * @return      不重复的数
     */
    public static int lackNum (Set<Integer> all) {
        int i = RandomUtil.randomInt(SpartaConstant.MIN, SpartaConstant.MAX);
        while (all.contains(i)) {
            i = RandomUtil.randomInt(SpartaConstant.MIN, SpartaConstant.MAX);
        }
        return i;
    }
}
